﻿<!DOCTYPE html>
<html>
<head>
<style>
body {
  font-family: helvetica, arial, sans-serif;
  font-size: 14px;
  width: 300px;
}
ul{
	list-style: url('icon-fav-collapsed.png') inside;
	padding-left: 14px;
	margin-left: 0;	
}
ul.visible {display:block;}
ul.hidden {display:none;}
a{
	cursor: pointer;
	text-decoration:none;
	color: #000
}
img{vertical-align: middle}
</style>
<script type="text/javascript">
//收藏XML地址
var favXmlUrl='http://fav.maxthon.cn/api/fav/';
var favPar='read?uid=';
var uid;
var req;

//获取收藏XML
function init(){
	req = new XMLHttpRequest();
	//获取uid	
	req.onerror = handleError;
	req.open("GET", favXmlUrl, false);
	req.send(null);
	//addt(req.responseText);
	if (get_uid(req.responseText)) {
		req.onload = handleResponse;
		req.open("GET", favXmlUrl+favPar+uid, true);
		req.send(null);
	}else{
		chrome.tabs.create({url: favXmlUrl});
		handleFeedParsingFailed("未登录傲游在线收藏！");
	}	
}

function get_uid(tt){
	var p = tt.indexOf("uid");
	if  (p>=0){
		p=p+5;
		var l = tt.indexOf(",",p);
		uid = tt.slice(p,l);
	}
	return	(p>=0);
}

// XML解析错误
function handleFeedParsingFailed(error) {
  var fav = document.getElementById("fav");
  fav.className = "error";
  fav.innerText = "错误: " + error;
}

// Handles errors during the XMLHttpRequest.
function handleError() {
  handleFeedParsingFailed('获取收藏信息失败！');
}

// Handles parsing the feed data we got back from XMLHttpRequest.
function handleResponse() {
  var doc = req.responseXML;
  if (!doc) {
	handleFeedParsingFailed("非有效收藏信息.");//未登录情况下会出现
	return;
  }
  var fav = document.getElementById("fav");
  var favorites = doc.getElementsByTagName("favorites");
  build(fav,favorites[0]);
}

function build(dd,ff){//在DOM节点dd下增加文件夹ff的收藏内容
    var ns = ff.childNodes;

	for (var i=0,f;f=ns[i];i++) {//f无效时结束循环
		if (f.getAttribute("r")!="Trash" && f.getAttribute("t")!="__groups") {//不显示回收站内容

			var d = document.createElement("li");
			dd.appendChild(d);
			var a = document.createElement("a");
			d.appendChild(a);
			if (f.tagName=="e"){
				//链接
				a.href = f.getAttribute("u");
				a.target = "_blank";				
				a.innerText = short_fav(f.getAttribute("t"));
				//a.innerHTML = '<img alt="" src="http://www.google.com/s2/favicons?domain_url='+f.getAttribute("u")+'" />'+short_fav(f.getAttribute("t"));
			}else{
				//文件夹
				a.innerHTML = '<img alt="" src="icon-fav-folder.png" />'+ f.getAttribute("t");
				var fd =document.createElement("ul");//下级收藏及文件夹
				build(fd,f);
				fd.className = "hidden";
				d.appendChild(fd);
				a.onclick = sh;
			}
		}
	}
}

function sh(event){
	var l = event.currentTarget.parentNode.lastChild;
	if (l.className=="visible"){
		l.className="hidden";
	}else{
		l.className="visible";
	}
}

function short_fav(t){
	if (t.length>20)
	{
		var s=t.slice(0,17)+"...";
		return s;
	}
	else
	{
		return t;
	}
}

function addt(tt){
	var i = document.getElementById("t");
	i.innerHTML=i.innerHTML+"<br>"+tt; 
}

</script>

</head>
<body onload="init();">
 	<div id="t"> 傲游在线收藏 </div>
	<ul id="fav"></ul>
</body>
</html>

